<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

/*
 * do basic housekeeping tasks
 */

require_once('admin.php');

$title = 'Options';
$this_file = 'lightpress/wp-plugin/options.php';
$parent_file = 'lightpress/wp-plugin/main.php';

$submenu[$this_file][] = array('LightPress', 'manage_options', 'lightpress/wp-plugin/options.php', 'LightPress');

/*
 * instantiate LightPressOptions and the Template object
 */

if (!isset($lp_opts)) {
    require_once 'LightPressOptions.php';
    $lp_opts = new LightPressOptions();
} else if (!is_object($lp_opts) || !is_subclass_of($lp_opts, 'LightPressOptions')) {
    $lp_opts = new LightPressOptions();
}

require_once implode(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), 'classes', 'Template.php'));
$tpl =& new Template(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'templates-'.__('en_US','lp'))));

$tpl->setFile('main', 'themes.html');

if (isset($_POST['theme'])) {
    $lp_opts->updateOption('lp_opt_template', $_POST['theme']);
} else {
    $tpl->setBlock('main', 'message', 'MESSAGE');
}

$themes_dir = $lp_opts->basedir . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR;

$current_theme = get_option('lp_opt_template');
$current_theme_preview = $themes_dir . $current_theme . DIRECTORY_SEPARATOR . 'preview.jpg';

$tpl_vars = array(
    'url'                   => get_option('siteurl'),
    'current_theme'         => ucwords($current_theme),
    'current_theme_preview' => is_file($current_theme_preview) && is_readable($current_theme_preview) ? "$current_theme/preview.jpg" : 'preview.jpg',
    'current_theme_readme'  => '',
    'MESSAGE'               => '',
    'THEME'                 => '',
);

$theme_readme = $themes_dir . get_option('lp_opt_template') . DIRECTORY_SEPARATOR . 'readme.txt';
if (is_file($theme_readme) && is_readable($theme_readme))
    $tpl_vars['current_theme_readme'] = implode('', file($theme_readme));

// detect valid themes
$lang = explode('_', get_option('lp_opt_lang'),1);
$charset = get_option('lp_opt_charset');
$theme_subdir = "${lang}.$charset";
$themes = array();
if (is_dir($themes_dir)) {
    $themes_dirp = opendir($themes_dir);
    while (($theme_dir = readdir($themes_dirp)) !== false) {
        if ($theme_dir[0] == '.')
            continue;
        if ($theme_dir == $current_theme)
            continue;
        $theme_path = $themes_dir . $theme_dir . DIRECTORY_SEPARATOR;
        if (!is_dir($theme_path . $theme_subdir))
             continue;
            
        $themes[] = array(
            'theme'=>ucwords($theme_dir),
            'theme_preview'=>file_exists($theme_path . 'preview.jpg') ? "$theme_dir/preview.jpg" : 'preview.jpg',
            'theme_readme'=>file_exists($theme_path . 'readme.txt') && is_readable($theme_path . 'readme.txt') ? implode('', file($theme_path . 'readme.txt')) : '');
    }
}
// wenn es weitere gibt, setz den THEME-Block
// if ($themes)) {
  $tpl->setBlock('main', 'theme', 'THEME');
  foreach ($themes as $t) {
    $tpl->setVar($t);
    $tpl->parse('THEME', 'theme', true);
  }
// }

$tpl->setVar($tpl_vars);

$out =& $tpl->parse('MAIN', 'main');
echo $out;

?>
